package com.mg.smarttask.client;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;

public class AddNote extends Composite {
	protected TextArea richTextArea = new TextArea();
	TextBox titleBox = new TextBox();
	private CheckBox shareCbx = new CheckBox("Partager cette note avec tous les membres smarttasks.");
	private NotesWindow notesWindow;
	private HorizontalPanel buttonPan = new HorizontalPanel();
	private Button validateAdd = new Button();
	private Button cancelAdd = new Button();
	
	public AddNote(NotesWindow  notesWindow) {
		
		buttonPan.add(validateAdd);
		buttonPan.add(cancelAdd);
		validateAdd.setStyleName("btnDone");
		cancelAdd.setStyleName("btnDelete");
		validateAdd.addStyleName("done");
		
		//richTextArea.setStyleName("rightContent");
		this.notesWindow = notesWindow;
		VerticalPanel verticalPanel = new VerticalPanel();
		VerticalPanel verticalPanel1 = new VerticalPanel();
		VerticalPanel verticalPanel2 = new VerticalPanel();
		//verticalPanel.setStyleName("addNotePanel");
		
		initWidget(verticalPanel);
		
		richTextArea.setFocus(true);
		richTextArea.setStyleName("gwt-TextArea");
		
	//	richTextArea.addKeyUpHandler(new KeyUpHandler() {
		//	public void onKeyUp(KeyUpEvent event) {
			//	int	keyCode = event.getNativeKeyCode();
				//if (keyCode == KeyCodes.KEY_ENTER){
					//saveNote();					
			//	} else if(keyCode == KeyCodes.KEY_ESCAPE){
					//removeEditor();
				//}
		//	}
	//	});
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		HorizontalPanel horizontalPanel2 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		verticalPanel.add(horizontalPanel2);
		
		HTML lblTitre = new HTML("<p> Ajout </p> <p> note </p>");
		lblTitre.setStyleName("dateCSS");
		lblTitre.addStyleName("ajoutNoteCSS");
		lblTitre.setWidth("55px");
		verticalPanel1.add(lblTitre);
		verticalPanel1.setCellHorizontalAlignment(lblTitre, HasHorizontalAlignment.ALIGN_CENTER);

		HorizontalPanel titleBoxPanel = new HorizontalPanel();
		HTML titleBoxTitle = new HTML("Titre : ");
		titleBoxTitle.setStyleName("noteCSS");
		titleBoxTitle.setWidth("70px");
		
		titleBoxPanel.add(titleBoxTitle);
		titleBoxPanel.add(titleBox);
		
		verticalPanel2.add(titleBoxPanel);
		
		HorizontalPanel contentBoxPanel = new HorizontalPanel();
		HTML contentBoxTitle = new HTML("Contenu : ");
		contentBoxTitle.setStyleName("noteCSS");
		contentBoxTitle.setWidth("70px");
		
		contentBoxPanel.add(contentBoxTitle);
		contentBoxPanel.add(richTextArea);
		
		verticalPanel2.add(contentBoxPanel);
		verticalPanel2.add(buttonPan);
		
		verticalPanel2.setStyleName("rightContent");
		
		horizontalPanel.add(verticalPanel1);
		horizontalPanel.add(verticalPanel2);
		
		verticalPanel.add(horizontalPanel);
		
		validateAdd.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				saveNote();					
			}
		});
		
		cancelAdd.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				removeEditor();					
			}
		});
		
		titleBox.setWidth("450px");
		richTextArea.setSize("450px", "100px");	
	}
	
	public void removeEditor(){
		this.removeFromParent();
	}
	
	public boolean saveNote(){
		String note = richTextArea.getText().trim();
		String title = titleBox.getText().trim();		
		if(title.equalsIgnoreCase("")) {
			Window.alert("Vous devez remplir le champ titre pour sauvegarder votre note");
			return false;
		}
		Note sNote = constructNote(title,note);
		sNote.setIsShared(shareCbx.getValue());
		createNewNote(sNote);
		richTextArea.setText("");
		titleBox.setText("");
		removeEditor();
		return true;
	}
	
	public void createNewNote(Note note){
		NoteService.Util.getInstance().createNote(note, new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				GWT.log("Error", caught);
				Window.alert("Un erreur est survenue");
			}

			@Override
			public void onSuccess(Boolean result) {
				refreshWindow();
			}
			
		});
	}
	public Note constructNote(String title, String description){
		Note note = new Note();
		note.setTitle(title);
		note.setDescription(description);
		return note;
	}
	public void refreshWindow(){
		removeEditor();
		this.notesWindow.showNotes.loadNotesFromServer();
		this.notesWindow.showNotes.updateNotePanel();
	}

}
